package com.igeek.collateral.service.Impl;
import com.igeek.collateral.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.collateral.constant.MessageConstant;
import com.igeek.collateral.entity.Agency;
import com.igeek.collateral.entity.Assess;
import com.igeek.collateral.mapper.AgencyMapper;
import com.igeek.collateral.service.IAgencyService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.collateral.vo.PageQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 评估机构 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-07-04
 */
@Service
public class AgencyServiceImpl extends ServiceImpl<AgencyMapper, Agency> implements IAgencyService {
@Autowired
private AgencyMapper agencyMapper;


@Autowired
    private  RedisTemplate redisTemplate;

//    @Override
//    public long testNumber(String numKey, long count) {
//        Object obj = redisTemplate.execute((RedisCallback<Object>) connection -> {
//            //1.加载Lua脚本
//            DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
//            redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("decrby.lua")));
////2.执行脚本  第一个参数：脚本的字节数组  第二个参数：Lua脚本执行后的返回值  第三个参数：键的数量
//            String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM"));
//            Object result = connection.scriptingCommands().eval(
//                    redisScript.getScriptAsString().getBytes(),
//                    ReturnType.INTEGER,
//                    1, numKey.getBytes(),String.valueOf(count).getBytes(),date.getBytes());
//            return result;
//        });
//        return Long.valueOf(obj.toString());
//    }
    @Transactional
    @Override
    public Page<Agency> findPage(PageQueryVO vo) {
    Integer currentPage = vo.getCurrentPage();
    Integer pageSize = vo.getPageSize();
    String queryString = vo.getQueryString();
    Page<Agency> page=new Page<>(currentPage,pageSize);
    LambdaQueryWrapper<Agency> lambdaQueryWrapper=new LambdaQueryWrapper<>();
    lambdaQueryWrapper.like(StringUtils.hasLength(queryString),Agency::getName,queryString);
    Page<Agency>agencyPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
    return agencyPage;
    }

    @Transactional
    @Override
    public boolean delete(Long id) {
        LambdaQueryWrapper<Agency> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Agency::getId,id);
        int i=this.baseMapper.delete(lambdaQueryWrapper);
        return i>0?true:false;
    }

    @Override
    public Agency selectAll(Long id) {
        Agency agency = this.agencyMapper.selectAll(id);
        return agency;
    }

    @Override
    public boolean deleteAll(Long id) {
       boolean flag=this.agencyMapper.deleteAll(id);
        return flag;
    }


}
